package com.xszx.knowledge.service.impl;

import com.xszx.knowledge.dto.DocReq;
import com.xszx.knowledge.entity.Content;
import com.xszx.knowledge.entity.Doc;
import com.xszx.knowledge.exception.DevException;
import com.xszx.knowledge.exception.DevExceptionCode;
import com.xszx.knowledge.mapper.ContentMapper;
import com.xszx.knowledge.mapper.DocMapper;
import com.xszx.knowledge.service.ContentService;
import com.xszx.knowledge.service.DocService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xszx.knowledge.util.RedisUtil;
import com.xszx.knowledge.util.RequestContext;
import com.xszx.knowledge.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 文档 服务实现类
 * </p>
 *
 * @author wencai
 * @since 2023/04/25 10:29
 */
@Service
@Transactional
public class DocServiceImpl extends ServiceImpl<DocMapper, Doc> implements DocService {
    @Resource
    private ContentMapper contentMapper;
    @Resource
    private DocMapper docMapper;
    @Resource
    SnowFlake snowFlake;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private WebSocketServiceImpl webSocketServiceImpl;

    /**
     * 1、同时进行数据的添加或者更新操作
     * 2、添加文档数据的时候需要同时将文档的内容页添加到数据表中
     *
     * @return
     */
    @Override
    public boolean saveDocAndContent(DocReq docReq) {
        if (!ObjectUtils.isEmpty(docReq) && docReq.getId() != null) {
            //更新操作
            boolean result = updateById(new Doc(docReq.getId(), docReq.getEbookId(), docReq.getParent(), docReq.getName(), docReq.getSort()));//更新doc表数据
            int resultUpdate = contentMapper.updateById(new Content(docReq.getId(), docReq.getContent()));//更新富文本内容
            if(resultUpdate == 0) {
                resultUpdate = contentMapper.insert(new Content(docReq.getId(), docReq.getContent()));
            }//添加文档内容
            return (result&&(resultUpdate>0))?true:false;
        } else {
            //雪花算法生成id
            long newId = snowFlake.nextId();
            //添加操作
            boolean result = save(new Doc(newId, docReq.getEbookId(), docReq.getParent(), docReq.getName(), docReq.getSort()));//更新文档数据
            int resultUpdate = contentMapper.insert(new Content(newId, docReq.getContent()));//添加文档内容
            return (result&&(resultUpdate>0))?true:false;
        }
    }

    /**
     * 更新阅读数
     * @param id
     */
    @Override
    public void updateViewCount(Long id) {
        docMapper.updateViewCount(id);
    }
    /**
     * 更新点赞数
     * @param id
     */
    @Override
    public void updateVoteCount(Long id) {
        docMapper.updateVoteCount(id);
    }

    @Override
    public void vote(Long id) {
        //获取当前请求的IP地址
        String ip = RequestContext.getRemoteAddr();
        //判断当前的访问信息在redis中是否存在
        //指定当前的访问信息内容组成为： VOTE_DOC_ip_id
        if(redisUtil.validateRepeat("VOTE_DOC_"+ip+"_"+id,5000)){
            //获取当前文档的内容
            Content content =contentMapper.selectById(id);
            //有文档内容的话才可以点赞
            if(!ObjectUtils.isEmpty(content)){
                updateVoteCount(id);
            }
        }else{
            // return Result.error().message("您已点赞过,暂时不能点赞！");
            throw new DevException(DevExceptionCode.VOTE_REPEAT);
        }
        //查询当前的文章的信息
        Doc doc = docMapper.selectById(id);
        //向客户端(浏览器)发送消息
        webSocketServiceImpl.sendMessage("【"+doc.getName()+"】这篇文章、被点赞了!");
    }
}
